package com.ruoyi.project.park.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.project.park.domain.BizParkingCard;
import com.ruoyi.project.park.service.impl.BizParkingCardServiceImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.park.domain.BizPaymentRecords;
import com.ruoyi.project.park.service.IBizPaymentRecordsService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 充值记录Controller
 * 
 * @author ruoyi
 * @date 2024-04-17
 */
@RestController
@RequestMapping("/park/records")
public class BizPaymentRecordsController extends BaseController
{
    @Autowired
    private IBizPaymentRecordsService bizPaymentRecordsService;

    /**
     * 查询充值记录列表
     */
    @PreAuthorize("@ss.hasPermi('park:records:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizPaymentRecords bizPaymentRecords)
    {
        //非管理员用户只能查看自己的
        if(!getLoginUser().getUser().getUserId().equals(1L)){
            Long userId = getUserId();
            bizPaymentRecords.setUserId(String.valueOf(userId));
        }
        startPage();
        List<BizPaymentRecords> list = bizPaymentRecordsService.selectBizPaymentRecordsList(bizPaymentRecords);
        return getDataTable(list);
    }

    /**
     * 导出充值记录列表
     */
    @PreAuthorize("@ss.hasPermi('park:records:export')")
    @Log(title = "充值记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizPaymentRecords bizPaymentRecords)
    {
        List<BizPaymentRecords> list = bizPaymentRecordsService.selectBizPaymentRecordsList(bizPaymentRecords);
        ExcelUtil<BizPaymentRecords> util = new ExcelUtil<BizPaymentRecords>(BizPaymentRecords.class);
        util.exportExcel(response, list, "充值记录数据");
    }

    /**
     * 获取充值记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('park:records:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(bizPaymentRecordsService.selectBizPaymentRecordsById(id));
    }


    @Autowired
    private BizParkingCardServiceImpl bizParkingCardService;
    /**
     * 新增充值记录
     */
    @PreAuthorize("@ss.hasPermi('park:records:add')")
    @Log(title = "充值记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizPaymentRecords bizPaymentRecords)
    {
        //获取到充值的金额
        BigDecimal money = bizPaymentRecords.getAmount();
        //判断充值金额是否小于0 bigdecimal
        if(money.compareTo(BigDecimal.ZERO) <= 0){
            return AjaxResult.error("充值金额不能小于0");
        }
        //生成id
        bizPaymentRecords.setId(IdUtil.getSnowflakeNextIdStr());

        Long userId = getUserId();
        bizPaymentRecords.setUserId(String.valueOf(userId));
        //插入支付时间
        bizPaymentRecords.setPaymentDate(new Date());
        //1.去会员卡表里面查询出余额
        //当前的用户名 去 会员卡表根据会员卡编号查询
        BizParkingCard card = new BizParkingCard();
        card.setCardNumber(getUsername());
        List<BizParkingCard> bizParkingCards = bizParkingCardService.selectBizParkingCardList(card);
        BizParkingCard bizParkingCard = bizParkingCards.get(0);
        BigDecimal balance = bizParkingCard.getBalance();
        //money+balance
        balance = balance.add(money);
        bizPaymentRecords.setBalance(balance);
        //2.更新余额
        BizParkingCard card1 = new BizParkingCard();
        card1.setId(bizParkingCard.getId());
        card1.setBalance(balance);
        bizParkingCardService.updateBizParkingCard(card1);
        return toAjax(bizPaymentRecordsService.insertBizPaymentRecords(bizPaymentRecords));
    }

    /**
     * 修改充值记录
     */
    @PreAuthorize("@ss.hasPermi('park:records:edit')")
    @Log(title = "充值记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizPaymentRecords bizPaymentRecords)
    {
        return toAjax(bizPaymentRecordsService.updateBizPaymentRecords(bizPaymentRecords));
    }

    /**
     * 删除充值记录
     */
    @PreAuthorize("@ss.hasPermi('park:records:remove')")
    @Log(title = "充值记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(bizPaymentRecordsService.deleteBizPaymentRecordsByIds(ids));
    }
}
